package com.vancouver.knows.business.tracker;

import java.util.ArrayList;
import java.util.List;

import com.pangu.bean.PageableResult;
import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.CommentInfo;
import com.vancouver.knows.business.task.mark.FetchCommentsTaskMark;

/**
 * 获取活动列表
 * 
 * @author chenxunlin01
 * 
 */
public class FetchCommentsTracker extends ABInvokeTracker {

	public FetchCommentsTracker(BusinessModule module, IResultReceiver resultReceiver) {
		super(module, resultReceiver);
	}

	@Override
	protected void handleTaskRefresh(ATaskMark taskMark, OperateResult result) {
		FetchCommentsTaskMark fetchCommentsTaskMark = (FetchCommentsTaskMark) taskMark;
		module.getCacheManager().getCommentInfoMap().remove(fetchCommentsTaskMark.getPostId());
	}

	@Override
	public String TAG() {
		return FetchCommentsTracker.class.getSimpleName();
	}

	@Override
	public void handleResult(OperateResult result) {
		PageableResult pageableResult = (PageableResult) result.getResultData();
		if (pageableResult.getContent() != null) {
			List<CommentInfo> commentInfoList = (List<CommentInfo>) pageableResult.getContent();
			FetchCommentsTaskMark fetchCommentsTaskMark = (FetchCommentsTaskMark) result.getTaskMark();
			fetchCommentsTaskMark.setPageInfo(pageableResult.getPageInfo());
			List<CommentInfo> commentInfoListCache = module.getCacheManager().getCommentInfoMap()
					.get(fetchCommentsTaskMark.getPostId());
			if (commentInfoListCache == null) {
				commentInfoListCache = new ArrayList<CommentInfo>();
				module.getCacheManager().getCommentInfoMap()
						.put(fetchCommentsTaskMark.getPostId(), commentInfoListCache);
			}

			if (fetchCommentsTaskMark.getPageInfo().getFrom() == 0) {
				commentInfoListCache.clear();
			}
			commentInfoListCache.addAll(commentInfoList);
		}
	}
}
